<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/6/2
 * Time: 16:58
 * 功能: 项目管理
 */
class project extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('project_model');
    }

    // 项目管理列表
    public function index()
    {
        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        $data = $this->project_model->page('project',0,$page_num,$start_position); //limit查询的数据

        if($data[0]){
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }else{
            $viewData = [
                'list' => '',  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];
        }

        $this->load->view('admin/project/list', $viewData);

    }

    // 新增功能
    public function add(){
    	$post = $this->input->post();
        $data = [
            'code' 		=> $post['code'],
            'name' 		=> $post['name'],
            'source' 	=> $post['source'],
            'linkman' 	=> $post['linkMan'],	// 联络人
            'note' 		=> $post['note'],
            'createMan' => $_COOKIE['nickname'],
            'createTime'=> time()
        ];
        // 是否会有添加失败的情况?
        if(count(array_keys($data)) == count(array_filter($data))){
            $resultData = $this->project_model->addId($data);

            if($resultData){
                redirect('/admin/project/list?page=1');
            }
        }else{
            echo __alert('参数错误');
        }


    }

    // 新增项目页面
    public function addView(){
        $this->load->view('/admin/project/add');
    }

    // 编辑功能
    public function edit(){
        $where = [
            'id' => $this->input->post('id')
        ];
        //todo 更多字段
        $data = [
            'name' => $this->input->post('name'),
            'source' => $this->input->post('source'),
            'linkman' => $this->input->post('linkMan'),
            'note' => $this->input->post('note'),
        ];



        //是否会有添加失败的情况?
        $resultData =  $this->project_model->edit($data,$where);


//        print_r($resultData);
//        die();

        if($resultData){
            redirect('/admin/project/index?page=1');
        }else{
            echo __alert('修改失败,请重试');
        }

    }

    //编辑页面
    public function editView(){

        //读取用户名根据get id
        $arg = [
            'id' => $this->input->get('id')
        ];

        $data['list'] = $this->project_model->getByFieldQueryData('*',$arg,0);

        //如果不存在直接跳转到404页面
        if(!$data['list']){
            redirect('/home/error');
        }


//        print_r($data);
//        die();

        $this->load->view('/admin/project/edit',$data);
    }

    //删除功能,支持批量删除
    //TODO 其他单据有关联的，不允许删除
    public function remove(){
        $postData = $this->input->post();

        if($postData){
            if(!$postData){
                redirect('/admin/project/index?page=1');
            }

            $post = $this->input->post('del_id');
            foreach ($post as $v){
                $where = [
                    'id' => $v
                ];

                $this->project_model->remove($where);
            }
            redirect('/admin/project/index?page=1');

        }else{
            $data = [
                'id' => $this->input->get('id'),
            ];

            //是否会有添加失败的情况?
            $resultData =  $this->project_model->remove($data);
            if($resultData){
                redirect('/admin/project/index?page=1');
            }
        }

    }


    //todo 导出功能待定
    public function daochu(){
		$this->load->library('Excels');
		
		$data = $this->project_model->getData('*', 1);
		foreach($data as $k => $v) {
			$v['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
			$data[$k]['createTime'] = $v['createTime'];
		}
		// 表格标题
		$title = [
			0 => [
				0 => '序号',
				1 => '项目编码',
				2 => '项目名称',
				3 => '项目来源',
				4 => '联络人',
				5 => '备注',
				6 => '创建人',
				7 => '创建日期'
			]
		];
		$this->excels->exports($data, $title);
    }
}